package com.mc.fil.service.impl;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilMultimedia;
import com.mc.fil.domain.FilWatering;
import com.mc.fil.mapper.FilMultimediaMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilDryingConditionsMapper;
import com.mc.fil.domain.FilDryingConditions;
import com.mc.fil.service.IFilDryingConditionsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 晾晒情况 晾晒情况Service业务层处理
 *
 * @author mc
 * @date 2021-04-15
 */
@Service
public class FilDryingConditionsServiceImpl implements IFilDryingConditionsService
{
    @Autowired
    private FilDryingConditionsMapper filDryingConditionsMapper;
    @Autowired
    private FilMultimediaMapper multimediaMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询晾晒情况 晾晒情况
     *
     * @param id 晾晒情况 晾晒情况ID
     * @return 晾晒情况 晾晒情况
     */
    @Override
    public FilDryingConditions selectFilDryingConditionsById(Long id)
    {
        FilDryingConditions dryingConditions = filDryingConditionsMapper.selectFilDryingConditionsById(id);;
        FilMultimedia multimedia = new FilMultimedia();
        multimedia.setFilId(id);
        multimedia.setGenre("filDryingConditions");
        dryingConditions.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        return dryingConditions;
    }

    /**
     * 查询晾晒情况 晾晒情况列表
     *
     * @param filDryingConditions 晾晒情况 晾晒情况
     * @return 晾晒情况 晾晒情况
     */
    @Override
    public List<FilDryingConditions> selectFilDryingConditionsList(FilDryingConditions filDryingConditions)
    {
        List<FilDryingConditions> list = filDryingConditionsMapper.selectFilDryingConditionsList(filDryingConditions);
        for(FilDryingConditions dryingConditions : list){
            FilMultimedia multimedia = new FilMultimedia();
            multimedia.setFilId(dryingConditions.getId());
            multimedia.setGenre("filDryingConditions");
            dryingConditions.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        }
        return list;
    }

    /**
     * 新增晾晒情况 晾晒情况
     *
     * @param filDryingConditions 晾晒情况 晾晒情况
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFilDryingConditions(FilDryingConditions filDryingConditions)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filDryingConditions.setCreateTime(DateUtils.getNowDate());
        filDryingConditions.setCreatedBy(loginUser.getUsername());
        filDryingConditionsMapper.insertFilDryingConditions(filDryingConditions);
        List<FilMultimedia> list = filDryingConditions.getPhotos();
        for(FilMultimedia fil : list){
            fil.setFilId(filDryingConditions.getId());
            fil.setCreatedBy(loginUser.getUsername());
            fil.setCreatedTime(DateUtils.getNowDate());
            fil.setGenre("filDryingConditions");
            multimediaMapper.insertFilMultimedia(fil);
        }
        return 1;
    }

}